package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.annotation.LoginPermissions;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.redis.service.IStrategyStatisVORedisService;
import cn.wolfcode.wolf2w.redis.service.ITravelStatisVORedisService;
import cn.wolfcode.wolf2w.redis.service.IUserInfoRedisService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    private IUserInfoService userService;
    @Autowired
    private IUserInfoRedisService userInfoRedisService;
    @Autowired
    private IStrategyStatisVORedisService strategyStatisVORedisService;
    @Autowired
    private ITravelStatisVORedisService travelStatisVORedisService;
    
    

    @GetMapping("/list")
    public Object list(){
       return userService.list();
    }

    @GetMapping("/selectOne")
    public Object selectOne(String id){
        return userService.selectOne(id);
    }

    @GetMapping("/checkPhone")
    public Object checkPhone(String phone){
        //查询数据库是否有该手机号
        boolean checkPhoneResult = userService.checkPhone(phone);
        return !checkPhoneResult;
    }

    @GetMapping("/sendVerifyCode")
    public Object sendVerifyCode(String phone){
        userService.sendVerifyCode(phone);
        return JsonResult.success();
    }

    /**
     * 点注册来到这里进行注册验证
     * @return
     */
    @PostMapping("/regist")
    public Object regist(String phone, String nickname,String password,String rpassword,String verifyCode){

        userService.regist(phone, nickname, password, rpassword, verifyCode);

        return JsonResult.success();
    }


    /**
     * 账号登录
     * @return
     */
    @PostMapping("/login")
    public Object login(String username, String password){
        //调用登录方法并返回登录成功的用户
        UserInfo userInfo = userService.login(username,password);

        //把用户对象存在redis中
        String token = userInfoRedisService.setUserToken(userInfo);

        //新建一个map,把token和用户信息存进去,并返回给前端
        HashMap hashMap = new HashMap();
        hashMap.put("token", token);
        hashMap.put("user", userInfo);

        return JsonResult.success(hashMap);
    }

    /**
     * 第二次访问
     * @param request
     * @return
     */
    @LoginPermissions
    @GetMapping("/currentUser")
    public Object currentUser(HttpServletRequest request){
        String token = request.getHeader("token");
        UserInfo userInfo = userInfoRedisService.getUserByToken(token);

        return JsonResult.success(userInfo);
    }

    @GetMapping("/strategies/favor")
    public Object favor(String userId){
        List<String> strategyUserIds =  strategyStatisVORedisService.getstrategyUserIds(userId);

        return JsonResult.success(strategyUserIds);
    }


    @GetMapping("/travel/favor")
    public Object travelFavor(String userId){
        List<String> strategyUserIds =  travelStatisVORedisService.gettravelUserIds(userId);

        return JsonResult.success(strategyUserIds);
    }
}
